<?php $this->Html->addCrumb('Negotiations', '/products/negotiation_accepted'); ?>
<?php $this->Html->addCrumb('Accepted Negotiations'); ?>
<style type="text/css">
    td p span{color:#00B050;}
    td.vendor h2 {font-size:20px; color: #00B050;}
</style>
<div class="global-title">Accepted Negotiations</div>
<div><?php echo $this->element('admin/product/negotiation_menu'); ?></div>
<div id='message_success'></div>
<?php if (!empty($products)): ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="0"  class="negotiate-table">
        <thead>
            <tr>
                <th  bgcolor="#ccc">&nbsp;</th>
                <th  bgcolor="#ccc">Item #</th>
                <th  bgcolor="#ccc">Title</th>
                <th  bgcolor="#ccc">Actual Price</th>
                <th  bgcolor="#ccc">Latest Offered</th>
                <th  bgcolor="#ccc">
                    <?php if ($session->read('grouptype') == 'Buyers') { ?>
                        Seller ID
                        <?php
                    }
                    ?>
                    <?php if ($session->read('grouptype') == 'Sellers') { ?>
                        Buyer ID
                        <?php
                    }
                    ?>
                </th>
                <th class="actions"  bgcolor="#ccc"><?php __('Actions'); ?></th>
            </tr>

        </thead>
        <tbody>
            <?php
            $i = 0;
            //pr($products);
            foreach ($products as $product):
                $class = null;
                if ($i++ % 2 == 0)
                    $class = ' class="alt"';
                ?>

                <tr class="fullrow<?php echo $product['Product']['id']; ?>">

                    <td <?php echo $class; ?>  style="vertical-align: middle;"><?php echo'<img src="' . Configure::read('base_url') . 'img/uploads/' . $product['Product']['products_thumbnailImage'] . '" alt="' . $product['Product']['products_title'] . '" width="80" border="0" />'; ?></td>
                    <td <?php echo $class; ?>  style="vertical-align: middle;text-align: center;">
                        <?php echo $product['Product']['id']; ?>
                    </td>
                    <td  <?php echo $class; ?>  style="vertical-align: middle;text-align: center;"><?php echo $product['Product']['products_title']; ?></td>
                    <td <?php echo $class; ?>  style="vertical-align: middle;text-align: center;"><?php echo $product['Product']['products_price']; ?></td>
                    <td <?php echo $class; ?>  style="vertical-align: middle;text-align: center;"><?php echo $product['Negotiation']['last_offer']; ?></td>
                    <td <?php echo $class; ?>  style="vertical-align: middle;text-align: center;">
                        <?php
                        if ($session->read('grouptype') == 'Buyers') {
                            echo $product['User']['username'];
                        } elseif ($session->read('grouptype') == 'Sellers') {
                            echo $product['Buyer']['username'];
                        }
                        ?>
                    </td>

                    <td align="center"  <?php echo $class; ?> style="vertical-align: middle;text-align: center;">
                        <?php if ($session->read('grouptype') == 'Buyers') { ?>
                            <?php if ($product['Negotiation']['cart_chk'] != 1) { ?>
                                <input type="button" value="Add to Cart" name="add_to_cart" class="add_to_cart" pid="<?php echo $product['Product']['id']; ?>" />
                                <?php
                            }
                            ?>
                            <input type="button" value="Retract" name="retract" class="retract" pid="<?php echo $product['Product']['id']; ?>" />
                            <?php
                        }
                        ?>
                        <?php if ($session->read('grouptype') == 'Sellers') { ?>
                            <input type="button" value="Reject Negotiation" name="" size="10" onclick="showPop(this)"
                                   title="negotiate" action="reject_negotiate" elmid="<?php echo $product['Negotiation']['id']; ?>" controller="products" />
                                   <?php
                               }
                               ?>
                    </td>

                </tr>
                <tr class="fullrow<?php echo $product['Product']['id']; ?>">
                    <td colspan="7">
                        <h2 class="trigger"><a href="#">View History</a></h2>
                        <div class="toggle_container">
                            <div class="block">
                                <table cellspacing="0" width="100%">
                                    <tbody>

                                        <tr style="text-align:left;">
                                            <td>
                                                Date
                                            </td>
                                            <td>
                                                Offered
                                            </td>
                                            <td>
                                                Status
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <hr />
                                            </td>

                                        </tr>
                                        <?php
                                        if ($session->read('grouptype') == 'Sellers') {
                                            $history = $this->requestAction('/products/getnegotiation/' . $product['Product']['id'] . '/' . $product['Negotiation']['buyer_id']);
                                        } else {
                                            $history = $this->requestAction('/products/getnegotiation/' . $product['Product']['id']);
                                        }
                                        if (!empty($history)) {
                                            ?>

                                            <?php foreach ($history as $historyvalue): ?>
                                                <tr  style="text-align:left;">
                                                    <td>
                                                        <?php echo date('d-m-Y', strtotime($historyvalue['Negotiation']['created'])); ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $historyvalue['Negotiation']['last_offer']; ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $historyvalue['Negotiation']['status']; ?>
                                                    </td>
                                                </tr>
                                                <tr  style="text-align:left;">
                                                    <td colspan="3">
                                                        You wrote: <?php echo $historyvalue['Negotiation']['message']; ?>
                                                    </td>

                                                </tr>
                                                <tr  style="text-align:left;">
                                                    <td colspan="3">
                                                        Seller wrote: <?php echo $historyvalue['Negotiation']['seller_msg']; ?>
                                                    </td>

                                                </tr>
                                                <tr>
                                                    <td colspan="3">
                                                        <hr />
                                                    </td>

                                                </tr>
                                                <?php
                                            endforeach;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                    </td>
                </tr>

                <?php
            endforeach;
            ?>
        </tbody>
    </table>
    <div class="paging">
        <?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class' => 'disabled')); ?>
        | 	<?php echo $this->Paginator->numbers(); ?>
        |
        <?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled')); ?>
    </div>



<?php else: ?>
    <br /><br />
    <h2>No accepted negotiations found.</h2>
    <br /><br /><br />
<?php endif; ?>
<style type="text/css">
    h2.trigger {
        padding: 0 0 0 21px;
        margin: 0 0 5px 0;
        background: url(<?php echo $this->webroot; ?>img/tp.jpeg) no-repeat;
        /*	height: 46px;
                line-height: 46px;*/


        font-weight: normal;
        /*	float: left;*/
    }
    h2.trigger a {
        color: #000;
        text-decoration: none;
        display: block;
    }
    h2.trigger a:hover { color: #5e5e5e; }
    h2.active {background: url(<?php echo $this->webroot; ?>img/tm.jpeg) no-repeat;} /*--When toggle is triggered, it will shift the image to the bottom to show its "opened" state--*/
    .toggle_container {
        margin: 0 0 5px;
        padding: 0;
        border-top: 1px solid #d6d6d6;
        background: #f0f0f0  repeat-y left top;
        overflow: hidden;
        font-size: 1.2em;

        clear: both;
    }
    .toggle_container .block {
        padding: 20px; /*--Padding of Container--*/

    }
</style>
<script type="text/javascript">
    jQuery(document).ready(function(){

        //Hide (Collapse) the toggle containers on load
        jQuery(".toggle_container").hide();

        //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
        jQuery("h2.trigger").click(function(){
            jQuery(this).toggleClass("active").next().slideToggle("slow");
            return false; //Prevent the browser jump to the link anchor
        });

        //retrack
        jQuery(".retract").click(function(){
            var pid=jQuery(this).attr("pid");
            jQuery.ajax({
                type: 'GET',
                url: baseurl+"products/delete_negotiation/"+pid,
                success: function(data){
                    jQuery(".fullrow"+pid).remove();
                    jQuery('#message_success').html('Retract successfully.');
                    jQuery('#message_success').addClass('msg_success');
                    jQuery("#message_success").show();
                    jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                }
            });
       
        });

        //Add to cart
        jQuery(".add_to_cart").click(function(){
            var pid=jQuery(this).attr("pid");
 
            jQuery.ajax({
                type: 'GET',
                url: baseurl+"carts/addtocart/"+pid+"/1/"+'0/'+'1',
                dataType: 'json',
                success: function(data){
                    alert(data.msg);
   
                }
            });
        });
 
    });
</script>